home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / progrmng / stdio.sit / test stdio.c < prev   
Encoding:
C/C++ Source or Header  |  1989-05-27  |  2.6 KB  |  104 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Stdio Extensions Demo
  3.  * ⌐ 1989, Nigel Perry.
  4.  *
  5.  * There are three extensions to stdio:
  6.  *
  7.  * 1) Addition of %k format to printf to format int/long as chars.
  8.  *    This costs 102 bytes of code in printf.
  9.  * 2) Addition of ".SF" 'driver' to fopen() and friends.
  10.  *    If the file name is given as ".SF" then fopen() will call
  11.  *    standard file to obtain file name, SFGet is used for read or append
  12.  *    access, SFPut for write.
  13.  * 3) Addition of fdopen() and fdreopen().
  14.  *    Named after their Unix counterparts, these routines take a
  15.  *    volume reference number and a PASCAL string to identify the
  16.  *    file instead of the fopen/freopen C string.
  17.  *
  18.  *    2) & 3) add 140 bytes to the fopen() code.
  19.  *
  20.  */
  21.  
  22. #include <stdio+.h>
  23. #include <FileMgr.h>
  24. #include <HFS.h>
  25.  
  26. #define NIL ((void *)0)
  27.  
  28. OSErr fpath(FILE *, int *, StringPtr);
  29.  
  30. main()
  31. {    FILE *in, *out;
  32.     int c2;
  33.     long c4;
  34.     char line[80];
  35.     Str255 fname;
  36.     int fvol;
  37.     OSErr err;
  38.     extern int errno;
  39.     
  40.     printf("Stdio Modifications Test/Demo\n\n");
  41.     
  42.     c2 = 'ok';
  43.     c4 = 'test';
  44.  
  45.     fputs("New %k format: printf(\":%3k:%.1k:%-4.3lk:%5lk:\\n\", c2, c2, c4, c4)\n", stdout);
  46.     fputs("Where int c2 = 'ok' and long c4 = 'test': ", stdout);
  47.     printf(":%3k:%.1k:%-4.3lk:%5lk:\n", c2, c2, c4, c4);
  48.     
  49.     fputs("\nTest of new 'driver' \".SF\", first select a file to write to:", stdout);
  50.     fflush(stdout);
  51.     
  52.     out = fopen(".SF", "w");
  53.     fputs("This file created/overwritten by fopen(\".SF\", \"w\")\n", out);
  54.     fclose(out);
  55.     
  56.     fputs("\n\nPlease select file again to append to it:", stdout);
  57.     fflush(stdout);
  58.     out = fopen(".SF", "a");
  59.     fputs("This line added after fopen(\".SF\", \"a\")\n", out);
  60.     fclose(out);
  61.     
  62.     fputs("\n\nNow select again to read back in:", stdout);
  63.     fflush(stdout);
  64.     in = fopen(".SF", "r");
  65.     putchar('\n');
  66.     while( fgets(line, 80, in) != NULL ) fputs(line, stdout);
  67.     (void)fpath(in, &fvol, fname);
  68.     fclose(in);
  69.     
  70.     fputs("\nFinally test fdopen() by rereading file:", stdout);
  71.     in = fdopen(fvol, fname, "r");
  72.     if(in == NULL) return;
  73.     putchar('\n');
  74.     while( fgets(line, 80, in) != NULL ) fputs(line, stdout);
  75.     fclose(in);
  76.     
  77.     
  78. }
  79.  
  80. /* HFS routine to work back from FILE * to volref/name */
  81. OSErr fpath(fl, pvol, pname) FILE *fl; int *pvol; StringPtr pname;
  82. {    FCBPBRec pb;
  83.     OSErr err;
  84.     WDPBRec wb;
  85.  
  86.     pb.ioRefNum = fl->refnum;
  87.     pb.ioCompletion = NIL;
  88.     pb.ioNamePtr = pname;
  89.     pb.ioVRefNum = 0;
  90.     pb.ioFCBIndx = 0;
  91.     if( (err = PBGetFCBInfo(&pb, false)) != noErr) return(err);
  92.     
  93.     wb.ioCompletion = NIL;
  94.     wb.ioNamePtr = NIL;
  95.     wb.ioVRefNum = pb.ioFCBVRefNum;
  96.     wb.ioWDDirID = pb.ioFCBParID;
  97.     wb.ioWDProcID = 'ERIK';
  98.     err = PBOpenWD(&wb, false);
  99.     
  100.     *pvol =  wb.ioVRefNum;
  101.     
  102.     return(err);
  103. }
  104.